import com.google.protobuf.InvalidProtocolBufferException;
import org.example.entity.CalculateProto;

import java.util.Arrays;

public class codeTest {
    public static void main(String[] args) throws InvalidProtocolBufferException {

        //初始化
        CalculateProto.Calculate.Builder calculate = CalculateProto.Calculate.newBuilder();
        calculate.setId(1);
        calculate.setSuccess(true);
        calculate.setMsg("success");
        calculate.setNumLeft(3d);
        calculate.setNumRight(4d);
        calculate.setOperator("add");
        calculate.setResult(7d);

        //序列化
        CalculateProto.Calculate calculate1 = calculate.build();



        byte[] bytes = calculate1.toByteArray();
        System.out.println("protobuf数据bytes[]:" + Arrays.toString(bytes));
        System.out.println("protobuf序列化大小: " + bytes.length);

        //反序列化
        CalculateProto.Calculate calculate2 = null;
        calculate2 = CalculateProto.Calculate.parseFrom(bytes);
        System.out.println(calculate2.getId());
        System.out.println(calculate2.getSuccess());
        System.out.println(calculate2.getMsg());
        System.out.println(calculate2.getNumLeft());
        System.out.println(calculate2.getNumRight());
        System.out.println(calculate2.getOperator());
        System.out.println(calculate2.getResult());
    }
}
